package com.Test.com;

import com.Test.com.MessageList;
import com.Test.com.SerialiableMessage;

import java.io.*;
import java.net.Socket;
import java.util.LinkedList;

public class ServerReceiveRunnable implements Runnable{
    private Socket accept;


    public ServerReceiveRunnable(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {
        try {
            System.out.println("*为"+accept.getInetAddress().getHostName()+"创建了一条接收线程!");
            ObjectInputStream objectInputStream=new ObjectInputStream(accept.getInputStream());
            SerialiableMessage messageLine;
            System.out.println("**********************************\n");

            /*****************************************/
            while ((messageLine =(SerialiableMessage)objectInputStream.readObject()) != null) {
                System.out.println("[from] "+messageLine.getSendUserNmae()+"  [to] "+messageLine.getReceiveUserName());
                System.out.println("[message]:  "+messageLine.getMessage());
                LinkedList<SerialiableMessage> messagesLinkList = MessageList.gainMessageList();
                messagesLinkList.add(messageLine);
                System.out.println("\n");
                System.out.println("**********************************\n");
            }
            objectInputStream.close();
        } catch (IOException e) {
        } catch (ClassNotFoundException e) {
        }finally {
            try {
                accept.close();
            } catch (IOException e) {
            }

        }
    }
}
